//
//  Math.m
//  HTMAnimations
//
//  Created by Ting Han on 2017/9/18.
//  Copyright © 2017年 Ting Han. All rights reserved.
//

#import "Math.h"

@implementation Math

+ (CGFloat)degreeFromRadian:(CGFloat)radian{
    return ((radian)*(180.0/M_PI));
}
+ (CGFloat)radianFromDegree:(CGFloat)degree{
    return ((degree)*M_PI/180.0);
}
+ (CGFloat)radianValueFromTanSideA:(CGFloat)sideA ToSideB:(CGFloat)sideB{
    return atan2f(sideA, sideB);
}

#pragma mark --- 
/*
 计算斜率
 */
CGFloat calculateSloap(CGFloat x1,CGFloat y1,CGFloat x2,CGFloat y2){
    if (x1 == x2) {
        return 0;
    }
    return (y2 - y1)/(x2 - x1);
}
/*
 计算常数
 */
CGFloat calculateConstant(CGFloat x1,CGFloat y1,CGFloat x2,CGFloat y2){
    if (x1 == x2) {
        return 0;
    }
    return (y1*(x2 - x1) - x1*(y2 - y1)) / (x2 - x1);
}

+ (instancetype)mathOnceLinearEquationWithPointA:(MATHPoint)pointA PointB:(MATHPoint)pointB{
    CGFloat x1 = pointA.x;
    CGFloat y1 = pointA.y;
    CGFloat x2 = pointB.x;
    CGFloat y2 = pointB.y;
    Math *math = [[[self class]alloc]init];
    math.k = calculateSloap(x1, y1, x2, y2);
    math.b = calculateConstant(x1, y1, x2, y2);
    return math;
}

- (CGFloat)xValueWhenYEqual:(CGFloat)yValue{
    if (_k == 0) {
        
        return 0;
    }
    
    return (yValue - _b) / _k;
}
- (CGFloat)yValueWhenXEqual:(CGFloat)xValue{
    return _k * xValue + _b;
}

#pragma mark --- 
+ (CGSize)resetFromSize:(CGSize)size withFixedWidth:(CGFloat)width{
    CGFloat newHeight = size.height * (width / size.width);
    CGSize  newSize   = CGSizeMake(width, newHeight);
    
    return newSize;
}
+ (CGSize)resetFromSize:(CGSize)size withFixedHeight:(CGFloat)height{
    float  newWidth = size.width * (height / size.height);
    CGSize newSize  = CGSizeMake(newWidth, height);
    
    return newSize;
}
@end
